//
//  SSSUploadMerchantBaseInfoCell.m
//  SSSAllRollAgency
//
//  Created by 谢曦 on 2017/3/14.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "SSSInfoBaseTableViewCell.h"
#import "Masonry.h"
#import "RightToLeftTextField.h"
#define kFont [UIFont systemFontOfSize:13];
#define kTextColor [UIColor colorWithHex:@"6D6D6D"]
@interface SSSInfoBaseTableViewCell()
@property (nonatomic,weak) RightToLeftTextField *rightTextField;
@property (nonatomic,weak) UIImageView *pic;
@property (nonatomic,weak) UILabel *titleLable;
@property (nonatomic,weak) UIImageView *arrowImage;
@property (nonatomic,weak) UILabel *rightLable;
@end
@implementation SSSInfoBaseTableViewCell
+(instancetype)cellForTableView:(UITableView *)tableView withIdentifier:(NSString *)identifier{
    SSSInfoBaseTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier ];
    if (!cell) {
        cell=[[SSSInfoBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        [cell initialAppearance];
        
    }
    return cell;
}
-(void)initialAppearance{
    
    UIImageView *imageView=[UIImageView new];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.mas_equalTo(0);
    }];
    _pic=imageView;
    
    UILabel *titleLable=[UILabel new];
    titleLable.font=kFont;
    [self addSubview:titleLable];
    titleLable.textColor=kTextColor;
    [titleLable sizeToFit];
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(imageView.mas_right).with.offset(15);
    }];
    
    _titleLable=titleLable;
    
    UIImageView *arrow=[UIImageView new];
    arrow.image=[UIImage imageNamed:@"icon_arrow_right_gray"];
    [self addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(arrow.image.size);
    }];
    _arrowImage=arrow;
    
    
    RightToLeftTextField *rightTextFiled=[RightToLeftTextField new];
    [self addSubview:rightTextFiled];
    rightTextFiled.textColor=kTextColor;
    [rightTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self);
        make.right.mas_equalTo(0);
        //           make.left.equalTo(titleLable.mas_right).with.offset(20);
    }];
    rightTextFiled.font=kFont;
    _rightTextField=rightTextFiled;
    
    
    
    UILabel *rightLable=[UILabel new];
    rightLable.textColor=kTextColor;
    [self addSubview:rightLable];
    [rightLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(arrow.mas_left).with.offset(-5);
        
        
    }];
    rightLable.font=kFont;
    _rightLable=rightLable;
    
    
    
    //    UISwitch *rightSwitch=[UISwitch new];
    //    [self.contentView addSubview:rightSwitch];
    //    [rightSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.right.equalTo(self).with.offset(-10);
    //        make.centerY.equalTo(self);
    //
    //    }];
    //    [rightSwitch addTarget:self action:@selector(rightSwitchValueChange:) forControlEvents:UIControlEventValueChanged];
    //    _rightSwitch=rightSwitch;
    
}
#pragma mark - note
//聚焦调用
-(void)textFeildChange:(NSNotification *)note{
    _SSSInfoBaseTableViewCellTextFieldChangeBlock?_SSSInfoBaseTableViewCellTextFieldChangeBlock(_rightTextField):nil;
    
}

#pragma mark -
-(void)updateRightText:(NSString *)text{
    _configModel.rightText=text;
    _rightLable.text=text;
    _rightTextField.text=text;
}
#pragma mark - setter getter
-(void)setCellAccessoryType:(SSSInfoBaseCellAccessoryType)cellAccessoryType{
    _cellAccessoryType=cellAccessoryType;
    switch (cellAccessoryType) {
        case SSSInfoBaseCellAccessoryTypeTextField:
        {
            _rightTextField.hidden=NO;
            _rightLable.hidden=YES;
            _arrowImage.hidden=YES;
            _customAccessoryView.hidden=YES;
        }
            break;
        case SSSInfoBaseCellAccessoryTypeLable:
        {
            _rightTextField.hidden=YES;
            _rightLable.hidden=NO;
            _arrowImage.hidden=YES;
            _customAccessoryView.hidden=YES;
        }
            
            break;
        case SSSInfoBaseCellAccessoryTypeLableWithArrow:
        {
            _rightTextField.hidden=YES;
            _arrowImage.hidden=NO;
            _rightLable.hidden=NO;
            _customAccessoryView.hidden=YES;
        }
            break;
        case SSSInfoBaseCellAccessoryTypeArrow:
        {
            _rightTextField.hidden=YES;
            _arrowImage.hidden=NO;
            _rightLable.hidden=YES;
        }
            break;
        case SSSInfoBaseCellAccessoryTypeNone:
        {
            _rightTextField.hidden=YES;
            _arrowImage.hidden=YES;
            _rightLable.hidden=YES;
            _customAccessoryView.hidden=YES;
        }
            break;
        case SSSInfoBaseCellAccessoryTypeCustom:
        {
            _rightTextField.hidden=YES;
            _arrowImage.hidden=YES;
            _rightLable.hidden=YES;
            _customAccessoryView.hidden=NO;
        }
        break;

        default:
            break;
    }
    
}
-(void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder=placeHolder;
    //重新设置placeholder的值 会覆盖颜色
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeHolder attributes:
                                      @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                        NSFontAttributeName:_rightTextField.font
                                        }];
    _rightTextField.attributedPlaceholder = attrString;
    [_rightTextField setValue:[UIColor whiteColor]forKeyPath:@"_placeholderLabel.textColor"];
    _rightLable.text=placeHolder;
    
}
-(void)setRightText:(NSString *)rightText{
    _rightText=rightText;
    _rightTextField.text=rightText;
    _rightLable.text=rightText;
}
-(void)setTitle:(NSString *)title{
    _title=title;
    _titleLable.text=title;
}
-(void)setImageName:(NSString *)imageName{
    _imageName=imageName;
    _pic.image=[UIImage imageNamed:imageName];
}


-(void)setConfigModel:(SSSCellBaseConfigModel *)configModel{
    _configModel=configModel;
    self.cellAccessoryType=configModel.accessoryType;
    if (configModel.placeHolder.length>0) {
        self.placeHolder=configModel.placeHolder;
    }
    if (configModel.rightText.length>0) {
        self.rightText=configModel.rightText;
    }
    if (configModel.rightAttributeStr.length>0) {
        _rightLable.attributedText=configModel.rightAttributeStr;

    }
    self.imageName=configModel.imageName;
    self.title=configModel.title;
}
#pragma mark - setter getter
-(void)setCustomAccessoryView:(UIView *)customAccessoryView{
    _customAccessoryView=customAccessoryView;
    self.cellAccessoryType=SSSInfoBaseCellAccessoryTypeNone;
    [self.contentView addSubview:customAccessoryView];
    
    [customAccessoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(customAccessoryView.bounds.size);
        make.centerY.equalTo(self);
        make.right.mas_equalTo(-15);
    }];
    
    
    
    
}

@end
